package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author stone
 * @date 2021/11/23 10:42
 */
@RequestMapping("upload")
@RestController
public class UploadController {

    //请求参数名和Handler方法的形参名一致
    @RequestMapping("file")
    public BaseRespVo uploadFile(MultipartFile myfile) throws IOException {
        //原始上传的文件名
        String originalFilename = myfile.getOriginalFilename();
        //请求参数名 → Handler方法的形参名
        String name = myfile.getName(); //myfile
        //获得文件大小
        long size = myfile.getSize();
        //正文类型 → 文件类型
        String contentType = myfile.getContentType();

        //保存文件 → 以指定目录下指定的文件名来接收
        File file = new File("D:\\spring", originalFilename);
        //转化
        myfile.transferTo(file);
        return BaseRespVo.ok(null);
    }

    @RequestMapping("files")
    public BaseRespVo uploadFiles(MultipartFile[] myfiles) throws IOException {
        for (MultipartFile myfile : myfiles) {
            String originalFilename = myfile.getOriginalFilename();
            //保存文件 → 以指定目录下指定的文件名来接收
            File file = new File("D:\\spring", originalFilename);
            //转化
            myfile.transferTo(file);
        }
        return BaseRespVo.ok(null);
    }
}
